const util = require('../../../../../../../utils/util.js');
const api = require('../../../../../../../config/api.js');
const user = require('../../../../../../../services/user.js');

//获取应用实例
const app = getApp()
const openid = app.globalData.logininfo.openid;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    createDialogShow: false,// 控制分组命名的弹窗
    groupManagerShow: false,//控制分组管理的底部弹窗
    circleid: '',// 圈子id
    groupList: [],//分组信息
    groupName: "",// 分组名字
    groupIndex: "",//分组选中下标
    deleteGroupDialogShow: false,// 控制删除分组的提示框
    isPlacedtop: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    var circleid = options.circleid;
    _this.setData({
      circleid: circleid
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 获取分组信息
    var _this = this;
    _this.getGroup(false);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 分组命名弹窗
   */
  createDialogShow: function () {
    var _this = this;
    _this.setData({
      createDialogShow: true
    })
  },

  /**
   * 分组命名关闭弹窗
   */
  createDialogClose: function () {
    var _this = this;
    _this.setData({
      createDialogShow: false,
      groupName: "",
      isPlacedtop: ""
    })
  },

  /**
   * 分组命名提交
   */
  createDialogConfirm: function () {
    var _this = this;
    if (openid != null && openid != "") {
      // 已登录
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var data = {};
        var groupIndex = _this.data.groupIndex.toString();
        if (groupIndex.length > 0){
          data.groupid = _this.data.groupList[groupIndex].id;
        }
        data.openid = openid;
        data.isPlacedtop = _this.data.isPlacedtop;
        data.access_token = res.access_token;
        data.text = _this.data.groupName;
        data.circleid = _this.data.circleid;
        util.post(api.Home_SaveOrUpdateGroupApiUrl, data).then(function (res) {
          if (res.code == 200) {
            if (groupIndex.length > 0) {
              _this.getGroup(false);
            } else {
              _this.getGroup(true);
            }
            _this.createDialogClose();
            _this.groupManagerClose();
          }
        })
      })
    }
  },

  /**
   * 分组管理的底部弹窗弹出
   */
  groupManagerShow: function (e) {
    var _this = this;
    var index = e.currentTarget.dataset.index;
    _this.setData({
      groupManagerShow: true,
      groupIndex: index
    })
  },

  /**
   * 分组管理的底部弹窗关闭
   */
  groupManagerClose: function () {
    var _this = this;
    _this.setData({
      groupManagerShow: false,
      groupIndex: ""
    })
  },

  /**
   * 获取分组信息
   */
  getGroup: function (isNavigateToGroupUser) {
    var _this = this;
    var circleid = _this.data.circleid;
    if (openid != null && openid != "") {
      // 已登录
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var data = {};
        data.circleid = circleid;
        data.openid = openid;
        data.access_token = res.access_token;
        util.post(api.Home_GetGroupApiUrl, data).then(function (res) {
          if (res.code == 200) {
            var list = res.result.list;
            _this.setData({
              groupList: list
            })
            if (isNavigateToGroupUser) {
              _this.navigateToGroupUser(list[list.length - 1]);
            }
          }
        });
      })
    }
  },
  
  /**
   * 分组命名的input输入事件
   */
  groupNameBindInput: function(e){
    var _this = this;
    var groupName = e.detail.value;
    _this.setData({
      groupName: groupName
    })
  },

  /**
   * 跳转到添加成员分组的页面
   */
  navigateToGroupUser: function (group) {
    var _this = this;
    var circleid = _this.data.circleid;
    var groupList = _this.data.groupList;
    wx.navigateTo({
      url: '../group/groupUser/groupUser?circleid=' + circleid + '&groupList=' + JSON.stringify(groupList) + '&group=' + JSON.stringify(group)
    })
  },

  /**
   * 跳转到添加成员分组的页面
   */
  navigateToGroupInfo: function (e) {
    var _this = this;
    var index = e.currentTarget.dataset.index;
    var groupList = _this.data.groupList;
    var group = _this.data.groupList[index];
    var circleid = _this.data.circleid;
    wx.navigateTo({
      url: '../group/groupInfo/groupInfo?circleid=' + circleid + '&groupList=' + JSON.stringify(groupList) + '&group=' + JSON.stringify(group)
    })
  },

  /**
   * 添加成员
   */
  addUser: function(){
    var _this = this;
    var groupIndex = _this.data.groupIndex;
    var group = _this.data.groupList[groupIndex];
    _this.groupManagerClose();
    _this.navigateToGroupUser(group);
  },

  /**
   * 重命名
   */
  updateGroupName: function () {
    var _this = this;
    var groupIndex = _this.data.groupIndex;
    var groupName = _this.data.groupList[groupIndex].name;
    _this.setData({
      groupManagerShow: false,
      groupName: groupName
    })
    _this.createDialogShow();
  },

  /**
   * 删除分组弹出提示框弹出
   */
  deleteGroupDialogShow: function(){
    var _this = this;
    _this.setData({
      deleteGroupDialogShow: true,
      groupManagerShow: false
    })
  },

  /**
   * 删除分组弹出提示框关闭
   */
  deleteGroupDialogClose: function () {
    var _this = this;
    _this.setData({
      deleteGroupDialogShow: false,
      groupIndex: ""
    })
  },

  /**
   * 删除分组：提交
   */
  deleteGroupDialogConfirm: function(){
    var _this = this;
    if (openid != null && openid != "") {
      // 已登录
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var data = {};
        data.circleid = _this.data.circleid;
        data.groupid = _this.data.groupList[_this.data.groupIndex].id;
        data.openid = openid;
        data.access_token = res.access_token;
        util.post(api.Home_DeleteGroupApiUrl, data).then(function (res) {
          if (res.code == 200) {
            _this.deleteGroupDialogClose(); 
            _this.getGroup(false);
          }
        });
      })
    }
  },

  /**
   * 小组置顶
   */
  groupPlacedtop: function(){
    var _this = this;
    var isPlacedtop = _this.data.groupList[_this.data.groupIndex].isPlacedtop;
    _this.setData({
      isPlacedtop: isPlacedtop == 1 ? 0 : 1
    })
    _this.createDialogConfirm();
  }
})